package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.SpecService;
import com.dongdongshop.vo.SpecVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

@DubboService
@Service
public class SpecServiceImpl implements SpecService {
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    public List<TbSpecification> listSpec() {
        return tbSpecificationMapper.selectByExample(null);
    }
    @Transactional
    @Override
    public void addSpec(String specName, List<TbSpecificationOption> tbSpecificationOptions) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setSpecName(specName);
        tbSpecificationMapper.insertSelective(tbSpecification);

        Long specId = tbSpecification.getId();

        //使用java8特性,stream
        tbSpecificationOptions.stream().forEach((option) ->{
            option.setSpecId(specId);
            tbSpecificationOptionMapper.insertSelective(option);
        });
    }

    @Override
    @Transactional
    public SpecVo toUpdate(Long id) {
        SpecVo specVo = new SpecVo();
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        BeanUtils.copyProperties(tbSpecification,specVo);

        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(example);
        specVo.setSpecOptionList(tbSpecificationOptions);
        return specVo;
    }
    @Transactional
    @Override
    public void update(Long id, String specName, List<TbSpecificationOption> tbSpecificationOptions) {
        TbSpecification tbSpecification = new TbSpecification();
        tbSpecification.setId(id);
        tbSpecification.setSpecName(specName);
        tbSpecificationMapper.updateByPrimaryKey(tbSpecification);

        //根据specId删除信息
        TbSpecificationOptionExample example = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
        criteria.andSpecIdEqualTo(id);
        tbSpecificationOptionMapper.deleteByExample(example);

        tbSpecificationOptions.stream().forEach((option)->{
            option.setSpecId(id);
            tbSpecificationOptionMapper.insertSelective(option);
        });
    }

    @Override
    @Transactional
    public void delSpecs(List<Long> ids) {
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        tbSpecificationMapper.deleteByExample(example);

        TbSpecificationOptionExample example1 = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andSpecIdIn(ids);
        tbSpecificationOptionMapper.deleteByExample(example1);
    }
}
